var createError = require('http-errors');
var express = require('express');
const bodyParser = require('body-parser')
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
// cors同源策略
const cors = require('cors');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

//改写
var http = require('http');
var server = http.createServer(app);

// 解决同源
app.use(cors()) 
//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "content-type");
      res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
      res.header("X-Powered-By", ' 3.2.1')
      res.header("Content-Type", "application/json;charset=utf-8");
      next();
})

app.use(express.urlencoded({
	extended: false
}));
app.use(cookieParser());
//静态资源
app.use('/static', express.static(path.join(__dirname, 'public')));

//post请求
app.use(bodyParser.urlencoded({
	limit: '50mb',
	extended: true
}));


app.use(bodyParser.json({
	limit: '50mb'
}));
app.use('/blog/', indexRouter);
app.use('/blog/users', usersRouter);

server.listen("3000");
